package cn.itcast.Servlet;

import cn.itcast.domain.Cake;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/jian")
public class jian extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        List<Cake> cart = (List) session.getAttribute("cart");
        int id = Integer.parseInt(request.getParameter("id"));
        for (Cake cake : cart) {
            if (cake.getId()==id)
            {
                int totalprice=0;
                if (session.getAttribute("totalprice")!=null)
                    totalprice= (int) session.getAttribute("totalprice");
                totalprice-=cake.getPrice();
                session.setAttribute("totalprice",totalprice);
                cart.remove(cake);
                break;
            }
        }
        response.sendRedirect(request.getContextPath()+"/carts.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
